//
//  YBEarnTransferViewController.m
//  Nectar
//
//  Created by famer on 2023/11/10.
//  Copyright © 2023 asance. All rights reserved.
//

#import "YBEarnTransferViewController+LayoutConstraints.h"
#import "HomeCoinManager.h"
#import "RevDataGetTransferInDetail.h"
#import "YBEarnDetailTotalEarnViewController.h"
#import "YBEarnDetailWithdrawalTimesViewController.h"

@interface YBEarnTransferViewController ()

@end

@implementation YBEarnTransferViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = kWhiteColor;
    
    [self.view addSubview:self.navigationBar];
    [self.view addSubview:self.contentView];

    [self.contentView addSubview:self.totalEarnContentView];
    [self.totalEarnContentView addSubview:self.totalEarnHeader];
    [self.totalEarnContentView addSubview:self.withdrawableRow];
    [self.totalEarnContentView addSubview:self.pendingRow];
    [self.totalEarnContentView addSubview:self.withdrawnRow];
    
    [self.contentView addSubview:self.withdrawalTimesContentView];
    [self.withdrawalTimesContentView addSubview:self.withdrawalTimesHeader];
    [self.withdrawalTimesContentView addSubview:self.availableTimesRow];
    [self.withdrawalTimesContentView addSubview:self.usedTimesRow];
    
    [self setLayoutConstraints];
    
    [self updateUI];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    [self.navigationBar resetBarItemState];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self getTransferInDetail];
}

#pragma mark - Private Method
- (void)updateUI{
    self.totalEarnContentView.clipsToBounds = YES;
    self.totalEarnContentView.layer.cornerRadius = 10;
    
    self.withdrawalTimesContentView.clipsToBounds = YES;
    self.withdrawalTimesContentView.layer.cornerRadius = 10;
}


- (void)getTransferInDetail{
    [SVProgressHUD show];
    self.view.userInteractionEnabled = NO;
    NSMutableDictionary *params = [NSDictionary baseReqParams];
    [params setObject:[HomeCoinManager shareInstance].globalCoinType forKey:@"coinType"];
    NSString *urlString = [NSString stringWithFormat:@"%@",PathGetTransferInDetail];
    
    [[YBHttpRequestManager shareInstance] Post:urlString params:params success:^(NSObject *resultObject) {
        NSLog(@"getTransferInDetail: %@",resultObject);
        
        self.view.userInteractionEnabled = YES;
        [SVProgressHUD dismiss];
        
        //判断网络是否返回错误信息
        YBResponseModel *obj = [YBResponseModel initWithResponseObject:(NSDictionary *)resultObject];
        if([obj success]){
            RevDataGetTransferInDetail *result = [RevDataGetTransferInDetail mj_objectWithKeyValues:obj.data];
            
            self.totalEarnHeader.valueLabel.text = [NSString stringWithFormat:@"%@ USDT", result.totalIncome];
            
            self.withdrawableRow.valueLabel.text = [NSString stringWithFormat:@"%@ USDT", result.unExtractIncome];
            
            self.pendingRow.valueLabel.text = [NSString stringWithFormat:@"%@ USDT", result.pendingIncome];
            
            self.withdrawnRow.valueLabel.text = [NSString stringWithFormat:@"%@ USDT", result.extractIncome];

            
            self.withdrawalTimesHeader.valueLabel.text = [NSString stringWithFormat:@"%@", result.totalTimes];
            
            self.availableTimesRow.valueLabel.text = [NSString stringWithFormat:@"%@", result.times];

            self.usedTimesRow.valueLabel.text = [NSString stringWithFormat:@"%@", result.usedTimes];
        }
        
    } failure:^(NSError *error) {
        self.view.userInteractionEnabled = YES;
        YBErrorModel *yerror = [YBErrorModel ErrorObjForNSError:error];
    }];
}

- (void)toTotalEarnDetailViewController{
    YBEarnDetailTotalEarnViewController *viewcontroller = [[YBEarnDetailTotalEarnViewController alloc] init];
    viewcontroller.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:viewcontroller animated:YES];
}

- (void)toWithdrawalTimesViewController{
    YBEarnDetailWithdrawalTimesViewController *viewcontroller = [[YBEarnDetailWithdrawalTimesViewController alloc] init];
    viewcontroller.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:viewcontroller animated:YES];
}

#pragma mark Getter Setter
YBViewGetter(contentView, _contentView, kThemeBackground);
YBViewGetter(totalEarnContentView, _totalEarnContentView, [UIColor hexColor:@"FFFFFF"]);
YBViewGetter(withdrawalTimesContentView, _withdrawalTimesContentView, [UIColor hexColor:@"FFFFFF"]);


- (YBEarnDetailRow *)withdrawableRow{
    if(!_withdrawableRow){
        _withdrawableRow = [[YBEarnDetailRow alloc] initWithFrame:CGRectZero];
        _withdrawableRow.titleLabel.text = @"Withdrawable";
    }
    return  _withdrawableRow;
}

- (YBEarnDetailRow *)pendingRow{
    if(!_pendingRow){
        _pendingRow = [[YBEarnDetailRow alloc] initWithFrame:CGRectZero];
        _pendingRow.titleLabel.text = @"Pending";
    }
    return  _pendingRow;
}

- (YBEarnDetailRow *)withdrawnRow{
    if(!_withdrawnRow){
        _withdrawnRow = [[YBEarnDetailRow alloc] initWithFrame:CGRectZero];
        _withdrawnRow.titleLabel.text = @"Withdrawn";
    }
    return  _withdrawnRow;
}

- (YBEarnDetailRow *)availableTimesRow{
    if(!_availableTimesRow){
        _availableTimesRow = [[YBEarnDetailRow alloc] initWithFrame:CGRectZero];
        _availableTimesRow.titleLabel.text = @"Available times";
    }
    return  _availableTimesRow;
}

- (YBEarnDetailRow *)usedTimesRow{
    if(!_usedTimesRow){
        _usedTimesRow = [[YBEarnDetailRow alloc] initWithFrame:CGRectZero];
        _usedTimesRow.titleLabel.text = @"Used times";
    }
    return  _usedTimesRow;
}

- (YBEarnDetailHeader *)totalEarnHeader{
    if(!_totalEarnHeader){
        _totalEarnHeader = [[YBEarnDetailHeader alloc] initWithFrame:CGRectZero];
        _totalEarnHeader.titleLabel.text = @"Total Earn";
        __weak __typeof(&*self)weakSelf = self;
        _totalEarnHeader.actionBlock = ^{
            __strong __typeof(&*weakSelf)strongSelf = weakSelf;
            if(!strongSelf) return;
            //to detail
            [strongSelf toTotalEarnDetailViewController];
        };
    }
    return  _totalEarnHeader;
}

- (YBEarnDetailHeader *)withdrawalTimesHeader{
    if(!_withdrawalTimesHeader){
        _withdrawalTimesHeader = [[YBEarnDetailHeader alloc] initWithFrame:CGRectZero];
        _withdrawalTimesHeader.titleLabel.text = @"Withdrawal times";
        __weak __typeof(&*self)weakSelf = self;
        _withdrawalTimesHeader.actionBlock = ^{
            __strong __typeof(&*weakSelf)strongSelf = weakSelf;
            if(!strongSelf) return;
            //to detail
            [strongSelf toWithdrawalTimesViewController];
        };
    }
    return  _withdrawalTimesHeader;
}

- (YBNavigationBar *)navigationBar{
    if(!_navigationBar){
        _navigationBar = [[YBNavigationBar alloc] initWithFrame:CGRectZero];
        _navigationBar.backgroundColor = kWhiteColor;
        [_navigationBar setTitle:@"Detail" titleColor:kThemeBlackColor];
        [_navigationBar setLeftBarImage:@"icon_nav_back" forState:UIControlStateNormal];
        __weak __typeof(&*self)weakSelf = self;
        _navigationBar.onLeftBarButtonBlock = ^(){
            __strong __typeof(&*weakSelf)strongSelf = weakSelf;
            if(!strongSelf) return;
            
            [strongSelf.navigationController popViewControllerAnimated:YES];
        };
    }
    return _navigationBar;
}

@end
